<template>
    <!--发货地-->
    <div id="ShipAddressDetails">
        <v-details :detailData="inputData" :inputConfig="inputConfig" @pop="setData" :rules="rules"
                   ref="input"></v-details>
    </div>
</template>

<script>
import details from 'commons/Detail'
import baseMixin from "@/mixins/BaseMixin";

export default {
    name: "ShipAddressDetails",
    mixins: [baseMixin],
    props: {
        inputData: {
            type: Object
        }
    },

    data() {
        return {
            inputConfig: [
                [
                    {
                        type: "table",
                        label: this.$t('config.supplier'),
                        props: "sellername",
                        tableConfig: [
                            {
                                label: this.$t('config.supplier'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('config.company'),
                                prop: "company",
                                input: "input",
                            },
                            {
                                label: this.$t('config.city'),
                                prop: "city",
                                input: "input",
                            },
                            {
                                label: this.$t('config.provice'),
                                prop: "state",
                                input: "input",
                            },
                            {
                                label: this.$t('config.postcode'),
                                prop: "zip",
                                input: "input",
                            },
                        ],
                        url: `/queryStorer/5/?/${this.$store.state.wms}`
                    },
                    {
                        type: "input",
                        label: this.$t('config.address',{no:'1'}),
                        props: "selleraddress1"
                    },
                    {
                        type: "input",
                        label: this.$t('config.address',{no:'2'}),
                        props: "selleraddress2"
                    },
                    {
                        type: "input",
                        label: this.$t('config.address',{no:'3'}),
                        props: "selleraddress3"
                    },
                    {
                        type: "input",
                        label: this.$t('config.address',{no:'4'}),
                        props: "selleraddress4"
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('config.city'),
                        props: "sellercity"
                    },
                    {
                        type: "input",
                        label: this.$t('config.provice'),
                        props: "sellerstate"
                    },
                    {
                        type: "input",
                        label: this.$t('config.postcode'),
                        props: "sellerzip"
                    },
                    {
                        type: "input",
                        label: this.$t('in.sellervat'),
                        disabled: true,
                        props: "sellervat"
                    }
                ]
            ],
            rules: {
                sellername: [
                    {validator: this.sellername, trigger: 'blur'},
                ]

            },
        }
    },
    methods: {
        sellername(rule, value, callback) {
            if (value === '') {
                callback();
            } else {
                this.$api.get(`/isExists/${5}/${value}/${this.$store.state.wms}`).then(res => {
                    
                        if (parseInt(res.bean) <= 0) {
                            this.$message.error(`不存在的供应商:${value}`);
                            this.inputData[rule.field] = ""
                            callback(new Error())
                        } else {
                            if (res.data.address1) {
                                this.inputData.selleraddress1 = res.data.address1;
                                this.$set(this.inputData, "selleraddress1", res.data.address1)
                            }
                            if (res.data.address2) {
                                this.$set(this.inputData, "selleraddress2", res.data.address2)
                            }
                            if (res.data.address3) {
                                this.$set(this.inputData, "selleraddress3", res.data.address3)
                            }
                            if (res.data.address4) {
                                this.$set(this.inputData, "selleraddress4", res.data.address4)
                            }
                            if (res.data.city) {
                                this.$set(this.inputData, "sellercity", res.data.city)
                            }
                            if (res.data.state) {
                                this.$set(this.inputData, "sellerstate", res.data.state)
                            }
                            if (res.data.zip) {
                                this.$set(this.inputData, "sellerzip", res.data.zip)
                            }
                            callback()
                        }
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })
            }
        },
        setData(data, id, close, listId) {
            this.inputData.sellername = data.storerkey;
            if (data.address1) {
                this.inputData.selleraddress1 = data.address1;
                this.$set(this.inputData, "selleraddress1", data.address1)
            }
            if (data.address2) {
                this.$set(this.inputData, "selleraddress2", data.address2)
            }
            if (data.address3) {
                this.$set(this.inputData, "selleraddress3", data.address3)
            }
            if (data.address4) {
                this.$set(this.inputData, "selleraddress4", data.address4)
            }
            if (data.city) {
                this.$set(this.inputData, "sellercity", data.city)
            }
            if (data.state) {
                this.$set(this.inputData, "sellerstate", data.state)
            }
            if (data.zip) {
                this.$set(this.inputData, "sellerzip", data.zip)
            }
            close(false);
        }
    },
    mounted() {
    },
    components: {
        'v-details': details
    }
}
</script>

<style scoped>

</style>

